package com.yanqu.road.server.manger.activity.auto.send;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.activity.unionwar.UnionWarRankMember;
import com.yanqu.road.entity.enums.eUnionPosition;
import com.yanqu.road.entity.union.UnionInfo;
import com.yanqu.road.logic.bussiness.activity.UnionWarActivityBusiness;
import com.yanqu.road.pb.activity.AutoSendRewardProto;
import com.yanqu.road.server.manger.union.CrossUnionMgr;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：zfr
 * @date ：2021/8/12 17:25
 */
public class CrossUnionWarActivityAutoSend extends BaseAutoSendActivity {

    public CrossUnionWarActivityAutoSend(ActivityInfo activityInfo) {
        super(activityInfo);
    }

    @Override
    public void sendLocalUserRankReward() {

    }

    @Override
    public void sendLocalUnionRankReward() {

    }

    @Override
    protected boolean updateConditionData(UserActivityConditionData data) {
        return UnionWarActivityBusiness.updateUserActivityConditionData(data);
    }

    @Override
    public void sendConditionReward() {

    }

    @Override
    public List<UserActivityConditionData> getNeedSendUserActivityConditionData() {
        return null;
    }

    @Override
    public Map<String, UnionActivityConditionData> getUnionActivityConditionData(int conditionType) {
        return null;
    }

    @Override
    public Map<Long, UserActivityConditionData> getUserActivityRankConditionData(int activityId, int conditionType) {
        return UnionWarActivityBusiness.getUserActivityRankConditionData(activityId, conditionType);
    }

    @Override
    public void noticeCrossUnionRank() {
        noticeCrossUnionGroupRank();
    }

    /**
     * 发送跨服商会排行奖励
     */
    public void sendCrossUnionRankReward(List<AutoSendRewardProto.UnionRankMsg> dataList) {
        Map<Integer, List<AutoSendRewardProto.UnionRankMsg>> userRankMsgMap = new HashMap<>();
        for (AutoSendRewardProto.UnionRankMsg msg : dataList) {
            int conditionType = msg.getConditionType();
            String unionId = msg.getUnionId();
            UnionInfo unionInfo = CrossUnionMgr.getUnionInfo(unionId);
            if (unionInfo != null) {
                if (!userRankMsgMap.containsKey(conditionType)) {
                    userRankMsgMap.put(conditionType, new ArrayList<>());
                }
                List<AutoSendRewardProto.UnionRankMsg> list = userRankMsgMap.get(conditionType);
                list.add(msg);
            }
        }
        for (Map.Entry<Integer, List<AutoSendRewardProto.UnionRankMsg>> entry : userRankMsgMap.entrySet()) {
            Integer conditionType = entry.getKey();
            int phase = 0;
            if (conditionType == 1000) {
                phase = 1;
            } else if (conditionType == 1001) {
                phase = 2;
            }
            Map<Long, UserActivityConditionData> conditionDataMap = getUserActivityRankConditionData(activityInfo.getActivityId(), conditionType);
            List<AutoSendRewardProto.UnionRankMsg> rankMsgList = entry.getValue();
            for (AutoSendRewardProto.UnionRankMsg msg : rankMsgList) {
                String unionId = msg.getUnionId();
                Map<String, Map<Integer, Map<Long, UnionWarRankMember>>> canGetRankRewardMemberMap = UnionWarActivityBusiness.getUnionWarRankMemberMap(activityInfo.getActivityId());
                Map<Integer, Map<Long, UnionWarRankMember>> unionMap = canGetRankRewardMemberMap.get(unionId);
                if (unionMap == null) {
                    continue;
                }
                Map<Long, UnionWarRankMember> unionWarRankMemberMap = unionMap.get(phase);
                if (unionWarRankMemberMap == null) {
                    continue;
                }
                for (UserActivityConditionData data : conditionDataMap.values()) {
                    long userId = data.getUserId();
                    if (!unionWarRankMemberMap.containsKey(userId)) {
                        continue;
                    }
                    if (data.isGetReward()) {
                        continue;
                    }
                    data.setGetReward(true);
                    int rank = msg.getRank();
                    ActivityConditionInfo conditionInfo = getUserRankCondition(conditionType, rank);
                    if (conditionInfo != null && updateConditionData(data)) {
                        String rewardStr;
                        if (unionWarRankMemberMap.get(userId).getPositionId() == eUnionPosition.Master.getValue()) {
                            rewardStr = conditionInfo.getMasterRewardStr();
                        } else {
                            //根据职位获取奖励
                            rewardStr = conditionInfo.getRewardStr();
                        }
                        CrossUnionMgr.getRewardHonourItem(rewardStr, activityInfo,rank,msg.getUnionId(),userId);
                        rewardStr = CrossUnionMgr.removeHonourItem(rewardStr);
                        AutoSendActivity.getLogger().info("autoSend unionWarReward, user: {}, activity: {},conditionType: {}, rank: {}, reward: {}", userId, activityInfo.getActivityId(), conditionType, rank, rewardStr);
                        sendMailReward(userId, rewardStr);
                    }
                }
            }
        }
        AutoSendActivity.changeSendState(activityInfo.getActivityId(), 2, 1);
    }
}
